<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>员工管理</title>
    <!-- 引入样式 -->
    <!--<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
    <!-- 引入组件库 -->
    <!--    <script src="https://unpkg.com/element-ui/lib/index.js"></script>-->

    <link rel="stylesheet" href="../static/css/elementui.css">
    <link rel="stylesheet" href="../static/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../static/css/style.css">
    <script src="../static/js/vue.js"></script>
    <script src="../static/js/axios.js"></script>
    <script src="../static/js/elementui.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .el-table td, .el-table th {
            text-align: center;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>员工管理<small></small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>员工管理</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/login/toLogin">退出</a></el-breadcrumb-item>
        </el-breadcrumb>

    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <!--查询表单-->
                <el-form :inline="true">
                    <el-form-item label="员工名字：">
                        <el-input v-model="searchMap.name" placeholder="品牌名称" class="filter-item" style="width: 150px"></el-input>
                    </el-form-item>

                    <el-form-item label="用户性别：">
                        <el-select v-model="searchMap.sex" clearable placeholder="请选择" class="filter-item" style="width: 150px">
                            <el-option  label="男"   value="1"></el-option>
                            <el-option  label="女"   value="0"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="入职时间：">
                        <el-date-picker type="datetime" v-model="searchMap.ruzhiDateBegin" placeholder="起始时间" style="width: 150px"></el-date-picker>
                        --
                        <el-date-picker type="datetime" v-model="searchMap.ruzhiDateEnd" placeholder="结束时间" style="width: 150px"></el-date-picker>
                    </el-form-item>

                    <el-button class="dalfBut" @click="fetchData()" type="primary" plain icon="el-icon-search">查询</el-button>
                    <el-button type="primary" plain @click="formVisible=true;pojo={}" icon="el-icon-circle-plus">新增员工</el-button>
                    <el-button type="primary" plain @click="saveDept()" v-show="user.roleId==1" icon="el-icon-circle-plus-outline">新增部门</el-button>
                    <el-button type="primary" plain @click="saveJob()"  v-show="user.roleId==1" icon="el-icon-circle-check">新增职位</el-button>
                </el-form>

                <el-table :data="tableData" border  style="width: 100%">
                    <el-table-column prop="id" label="序号" align="center"></el-table-column>
                    <el-table-column prop="name" label="员工名字" ></el-table-column>
                    <el-table-column prop="sexStr" label="性别" ></el-table-column>
                    <el-table-column prop="brithDay" label="生日"  :formatter="dateFormat"> </el-table-column>
                    <el-table-column prop="ruzhiDate" label="入职时间" :formatter="dateFormat"></el-table-column>
                    <el-table-column prop="xueli" label="学历" ></el-table-column>
                    <el-table-column prop="jobName" label="岗位" ></el-table-column>
                    <el-table-column prop="deptName" label="部门" ></el-table-column>
                    <el-table-column prop="phone" label="手机号" ></el-table-column>
                    <el-table-column prop="emergencyContact" label="紧急联系人" ></el-table-column>
                    <el-table-column
                            label="操作" width="230">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row.id)" type="primary" plain icon="el-icon-edit">修改</el-button>
                            <el-button @click="dele(scope.row.id)" type="danger" plain icon="el-icon-delete">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--弹出编辑窗口-->
                <div class="add-form">
                    <el-dialog  title="编辑" :visible.sync="formVisible" >
                        <el-form label-width="100px">
                            <el-form-item label="员工名字:">
                                <el-input v-model="pojo.name" style="width: 150px" clearable></el-input>
                                <el-input v-model="pojo.id" style="width: 150px" type="hidden"></el-input>
                            </el-form-item>
                            <el-form-item label="性别:">
                                <el-radio v-model="pojo.sex" :label="1">男</el-radio>
                                <el-radio v-model="pojo.sex" :label="0">女</el-radio>
                            </el-form-item>
                            <el-form-item label="生日:"><el-date-picker type="datetime" v-model="pojo.brithDay" style="width: 150px"/></el-form-item>
                            <el-form-item label="入职时间:"><el-date-picker type="datetime" v-model="pojo.ruzhiDate" style="width: 150px"/></el-form-item>
                            <el-form-item label="学历:"><el-input v-model="pojo.xueli" style="width: 150px" clearable></el-input></el-form-item>
                            <el-form-item label="岗位:">
                                <el-select v-model="pojo.jobId" placeholder="请选择" class="filter-item">
                                    <el-option  v-for="item in jobList"
                                                :key="item.id"
                                                :label="item.jobName"
                                                :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="部门:">
                                <el-select v-model="pojo.deptId" placeholder="请选择" class="filter-item">
                                    <el-option  v-for="item in deptList"
                                                :key="item.id"
                                                :label="item.deptName"
                                                :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="手机号:"><el-input v-model="pojo.phone" style="width: 150px" clearable></el-input></el-form-item>
                            <el-form-item label="紧急联系人:"><el-input v-model="pojo.emergencyContact" style="width: 150px" clearable></el-input></el-form-item>
                            <el-form-item>
                                <el-button type="primary" plain @click="save()">保存</el-button>
                                <el-button type="danger" plain @click="formVisible = false" >关闭</el-button>
                            </el-form-item>
                        </el-form>
                    </el-dialog>
                </div>

                <div class="pagination-container">
                    <el-pagination
                            class="pagiantion"
                            @size-change="sizeChange"
                            @current-change="fetchData"
                            :current-page.sync="currentPage"
                            :page-sizes="[5, 10, 15, 20]"
                            :page-size="size"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                tableData: [],
                jobList:[],
                deptList:[],
                currentPage: 1,
                total: 10,
                size: 5,
                searchMap: {},
                pojo: {},
                formVisible: false,
                user: {},
            }
        },
        created() {
            this.queryUserRole();
            this.queryJobList();
            this.queryDeptList();
            this.fetchData();
        },
        methods: {
            queryUserRole(){
                axios.post(`/user/userRole`).then(response =>{
                    this.user =response.data;
                });
            },
            queryJobList(){
                axios.post(`/user/jobList`).then(response =>{
                    this.jobList =response.data;
                });
            },
            queryDeptList(){
                axios.post(`/user/deptList`).then(response =>{
                    this.deptList =response.data;
                });
            },
            sizeChange(v){
                this.size = v;
                this.fetchData();

            },
            fetchData() {
                axios.post(`/user/findPage?page=${this.currentPage}&size=${this.size}`, this.searchMap).then(response => {
                    this.tableData = response.data.rows;
                    this.total = response.data.total;
                });
            },
            edit(id){
                this.formVisible = true // 打开窗口
                // 调用查询
                axios.get(`/user/findById?id=${id}`).then(response => {
                    this.pojo = response.data;
                });
            },
            save (){
                // this.pojo.image= this.imageUrl; //如页面有图片上传功能放开注释
                axios.post(`/user/save`,this.pojo).then(response => {
                    this.fetchData (); //刷新列表
                    this.formVisible = false ;//关闭窗口
                });
            },
            dele(id){
                this.$confirm('确定要删除此记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then( () => {
                    axios.get(`/user/deleteById?id=${id}`).then(response => {
                        this.fetchData (); //刷新列表
                    })
                });
            },
            //时间格式化
            dateFormat:function(row,column){
                let property = row[column.property];
                let t =new Date(property);
                //var t=new Date(row.brithDay);//row 表示一行数据, updateTime 表示要格式化的字段名称
                //return t.getFullYear()+"-"+(t.getMonth()+1)+"-"+t.getDate()+" "+t.getHours()+":"+t.getMinutes()+":"+t.getSeconds()+"."+t.getMilliseconds();
                return t.getFullYear()+"-"+(t.getMonth()+1)+"-"+t.getDate();
            }
        }
    })
</script>
</html>